#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion

using UnityEngine;

namespace Neatly.Tween
{
    public enum EffectLoopMode
    {
        None,
        Loop,
        PingPong,
    }
    public class EffectTweenImp
    {
        private uint m_id;
        public uint id { get { return m_id; } }
        public GameObject gameObject;
        private EffectTweenObjectBase _mEffectTween;
        private bool m_Pause;
        private EffectLoopMode m_LoopMode = EffectLoopMode.None;
        private OnCompleteDel m_CompleteAction;
        public delegate void OnCompleteDel();
        public bool IsDestroy { get; private set; }
        public EffectTweenImp Init(uint uid, EffectTweenObjectBase effectTween)
        {
            m_id = uid;
            _mEffectTween = effectTween;
            gameObject = effectTween.gameObject;
            m_CompleteAction = null;
            m_Pause = false;
            IsDestroy = false;
            m_LoopMode = EffectLoopMode.None;
            return this;
        }

        public void Excute(float dt)
        {
            if (_mEffectTween.IsDestroy)
            {
                IsDestroy = true;
                return;
            }
            if (m_Pause)
                return;
            if (_mEffectTween.Excute(dt))
            {
                switch (m_LoopMode)
                {
                    case EffectLoopMode.Loop:
                        _mEffectTween.LoopInit();
                        return;
                    case EffectLoopMode.PingPong:
                        _mEffectTween.PingPongInit();
                        return;
                }
                IsDestroy = true;
            }
        }

        public EffectTweenImp SetLoopMode(EffectLoopMode mode)
        {
            m_LoopMode = mode;
            return this;
        }

        public EffectTweenImp SetLoop(bool isLoop = true)
        {
            if (isLoop)
            {
                m_LoopMode = EffectLoopMode.Loop;
            }
            return this;
        }

        public EffectTweenImp SetPingPong(bool isPingPong = true)
        {
            if (isPingPong)
            {
                m_LoopMode = EffectLoopMode.PingPong;
            }
            return this;
        }

        public EffectTweenImp SetOnComplete(OnCompleteDel completeDel)
        {
            m_CompleteAction = completeDel;
            return this;
        }

        public void Pause()
        {
            m_Pause = true;
        }

        public void Resume()
        {
            m_Pause = false;
        }

        public void Cancel()
        {
            m_CompleteAction = null;
            IsDestroy = true;
            m_id = 0;
        }

        public void Destroy()
        {
            if (!_mEffectTween.IsDestroy)
            {
                m_CompleteAction?.Invoke();
            }
            _mEffectTween.Destroy();
            EffectTween.Instance.ImplementPool.Recycle(this);
        }
    }
}
